﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace SharpGeo
{
    /// <summary>
    /// An lightweight immutable class to store geographic coordinates.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    [DebuggerDisplay("({Latitude},{Longitude})")]
    sealed public class GeographicCoordinate :
        IEquatable<IGeographicCoordinate>,
        IGeographicCoordinate
    {
        #region Constructors

        /// <summary>
        /// Constructs a GeographicCoordinate
        /// </summary>
        /// <param name="longitude">The longitude value</param>
        /// <param name="latitude">The latitude value</param>
        public GeographicCoordinate(double longitude, double latitude)
        {
            _longitude = longitude;
            _latitude = latitude;
        }

        /// <summary>
        /// Creates a clone of a passed IGeographicCoordinate
        /// </summary>
        /// <param name="coordinate">The IGeographicCoordinate to clone</param>
        public GeographicCoordinate(IGeographicCoordinate coordinate)
        {
            if (coordinate == null)
                throw new ArgumentNullException("coordinate",
                    Properties.Resources.GeographicCoordinateGeographicCoordinateNullArgumentException);

            _longitude = coordinate.Longitude;
            _latitude = coordinate.Latitude;
        }

        #endregion Constructors

        #region Fields

        private double _longitude;
        private double _latitude;

        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets the Longitude value
        /// </summary>
        public double Longitude { get { return _longitude; } }

        /// <summary>
        /// Gets the Latitude value
        /// </summary>
        public double Latitude { get { return _latitude; } }

        /// <summary>
        /// Gets the oordinate at the specified axis
        /// </summary>
        /// <param name="axis">The axis of the oordinate to retrieve</param>
        /// <returns>Returns the oordiante at the specified axis, if the axis not between 0 and 1, double.NaN is returned.</returns>
        public double this[int axis]
        {
            get
            {
                if (axis == 0)
                {
                    return _longitude;
                }
                else if (axis == 1)
                {
                    return _latitude;
                }
                else
                {
                    return double.NaN;
                }
            }
        }

        /// <summary>
        /// Gets the number of Axes (2)
        /// </summary>
        int ICoordinate.NumberOfAxes { get { return 2; } }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Tests for equality with the passed IGeographicCoordinate
        /// </summary>
        /// <param name="obj">The IGeographicCoordinate to test for equality with</param>
        /// <returns>Returns true if this GeographicCoordinate equals the passed IGeographicCoordinate </returns>
        public override bool Equals(object obj)
        {
            var other = obj as IGeographicCoordinate;

            if (obj == null || other == null)
                return false;

            return other.Longitude == Longitude && other.Latitude == Latitude;
        }

        /// <summary>
        /// Tests for equality with the passed IGeographicCoordinate
        /// </summary>
        /// <param name="other">The IGeographicCoordinate to test for equality with</param>
        /// <returns>Returns true if this GeographicCoordinate equals the passed IGeographicCoordinate </returns>
        public bool Equals(IGeographicCoordinate other)
        {
            if (other == null)
            {
                return false;
            }

            return other.Longitude == Longitude && other.Latitude == Latitude;
        }

        /// <summary>
        /// Converts this GeographicCoordinate to a string
        /// </summary>
        /// <returns>A string representing this GeographicCoordinate (Latitude, Longitude)</returns>
        public override string ToString()
        {
            return string.Format(System.Globalization.CultureInfo.InvariantCulture, "({1},{0})", Longitude, Latitude);
        }

        /// <summary>
        /// Returns a hashcode representing this GeographicCoordinate
        /// </summary>
        /// <returns>The computed hashcode representing this GeographicCoordinate</returns>
        public override int GetHashCode()
        {
            unchecked
            {
                long l = BitConverter.DoubleToInt64Bits(Latitude);
                l *= l * 31 ^ BitConverter.DoubleToInt64Bits(Longitude);
                return (int)((l >> 32) ^ l);
            }
        }

        #endregion Methods
    }
}